package com.rongshu.view.penpal.present;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.login.entity.PasswordLogin;
import com.rongshu.view.penpal.bean.AddNoticeBody;
import com.rongshu.view.penpal.bean.EditNoticeBody;
import com.rongshu.view.base.mvp.INetBean;
import com.utils.StringUtils;
import com.widget.toast.ToastUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2018/11/24.
 * description：
 */
public class AddPenpalPresenter extends BasePresenter<INetView> {

    public AddPenpalPresenter(Context context) {
        super(context);
    }

    public void addNotice(int penpalwill_id, String content) {
        AddNoticeBody body = new AddNoticeBody();
        body.setContents(StringUtils.toHTMLString(content));
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("penpalwill_id", penpalwill_id);
        RetorfitService.addPenpalNotice(UserInfoUtils.getUserId(), penpalwill_id,
                Common.encryptionSign(map), body)
                .compose(getView().<INetBean>bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                ToastUtils.show(object.getErrMsg());
                if (object.getErrCode() == 0) {
                    getView().success(object, 0);
                }
            }
        });
    }

    public void editNotice(int announCement_id, String content) {
        EditNoticeBody body = new EditNoticeBody();
        body.setContents(StringUtils.toHTMLString(content));
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("announCement_id", announCement_id);
        RetorfitService.editPenpalNotice(UserInfoUtils.getUserId(), announCement_id,
                Common.encryptionSign(map), body)
                .compose(getView().<INetBean>bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                ToastUtils.show(object.getErrMsg());
                if (object.getErrCode() == 0) {
                    getView().success(object, 0);
                }
            }
        });
    }

    public String replaceBlank(String src) {
        String dest = "";
        if (src != null) {
            Pattern pattern = Pattern.compile("\n|\\s*");
            Matcher matcher = pattern.matcher(src);
            dest = matcher.replaceAll("");
        }
        return dest;
    }
}
